package com.java2007.zhangyu.restaurant.controller.front;

import com.java2007.zhangyu.restaurant.constant.ServletConstant;
import com.java2007.zhangyu.restaurant.controller.BaseServlet;
import com.java2007.zhangyu.restaurant.entity.CartItem;
import com.java2007.zhangyu.restaurant.entity.Result;
import com.java2007.zhangyu.restaurant.pojo.Admin;
import com.java2007.zhangyu.restaurant.service.OrderService;
import com.java2007.zhangyu.restaurant.service.impl.OrderServiceImpl;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 订单控制器
 * @author zy
 * @Version
 * @date 2021/01/29
 */
@WebServlet("/order")
public class OrderController extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();

    /**
     * 生成订单
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String createOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();
        //得到保存在session中的数据
        Integer dtId = (Integer) session.getAttribute("tableId");

        List<CartItem> cart = (List<CartItem>) session.getAttribute("front_cart");

        if(null == cart || cart.size() <= 0){
            return "尚未选择菜品！！！";
        }

        Long cartTotalPrice = (Long) session.getAttribute("cart_total_price");

        Result result = orderService.createOrder(cart, cartTotalPrice, dtId);

        Map<String,Object> orderInfo = (Map<String, Object>) result.getData();
        session.setAttribute("oId", orderInfo.get("oId"));
        session.setAttribute("order_detail_list", orderInfo.get("orderDetailList"));
        session.setAttribute("order_total_price", orderInfo.get("total_price"));

        //清空餐车中的数据
        session.removeAttribute("front_cart");
        session.removeAttribute("cart_total_price");

        //重定向
        return ServletConstant.PRE_REDIRECT + "/front/detail/clientOrderList.jsp";
    }
}
